let gulp = require('gulp');
let conectuser = require('gulp-connect');
let proxy = require('http-proxy-middleware');
let sass = require('gulp-sass');
sass.compiler = require('node-sass');
let { paths, proxyList } = require('./gulp.config.js');

function style() {
    return gulp.src(paths.style.src)
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest(paths.style.dist))
        .pipe(conectuser.reload());
}
function javascript() {
    return gulp.src(paths.javascript.src)
        .pipe(gulp.dest(paths.javascript.dist))
        .pipe(conectuser.reload());
}
function json() {
    return gulp.src(paths.json.src)
        .pipe(gulp.dest(paths.json.dist))
        .pipe(conectuser.reload());
}
function modules() {
    return gulp.src(paths.modules.src)
        .pipe(gulp.dest(paths.modules.dist))
        .pipe(conectuser.reload());
}
function html() {
    return gulp.src(paths.html.src)
        .pipe(gulp.dest(paths.html.dist))
        .pipe(conectuser.reload());
}
function php() {
    return gulp.src(paths.php.src)
        .pipe(gulp.dest(paths.php.dist))
        .pipe(conectuser.reload());
}
function connect() {
    conectuser.server({
        root: './dist',
        port: 4396,
        livereload: true,
        middleware: function (connect, opt) {
            return [
                proxy('/phplogin', {
                    target: 'http://localhost/phplogin',
                    changeOrigin: true,
                    pathRewrite: {
                        '^/phplogin': ''
                    },
                }
                ),
                proxy('/zc', {
                    target: 'http://www.zcool.com.cn/recommend/contentRecommend?pageSize=5&contentId=9196205_3&day=2019-07-24',
                    changeOrigin: true,
                    pathRewrite: {
                        '^/zc': ''
                    }
                })


            ]
        }
    })
}
function watch() {
    gulp.watch(paths.style.src, style);
    gulp.watch(paths.javascript.src, javascript);
    gulp.watch(paths.html.src, html);
    gulp.watch(paths.modules.src, modules);
    gulp.watch(paths.php.src, php);
    gulp.watch(paths.json.src, json);
}
let dev = gulp.series(gulp.parallel(connect, watch));
module.exports = dev;